import {
  newsDetails,
  likeNews,
  collectNews
} from '../../apis/index';
import {
  splicing,
  formatTime
} from '../../utils/util';
import {
  request
} from "../../utils/request";
const app = getApp();

Page({
  data: {
    newsDetails: {},
    authorizationShow: false,
    authorizationCallBack: null,
    domain: request.defaults.editorURL
  },

  onLoad(options) {
    this.setData({
      ...options
    });
    this.getNewsDetails(options.newsId);
  },

  onShareAppMessage() {
    return {
      title: this.data.newsDetails.title,
      path: `/pages/newsDetails/index?newsId=${this.data.newsId}`,
      imageUrl: this.data.newsDetails.image
    }
  },

  // 新闻详情
  getNewsDetails(id) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    newsDetails(id).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        data.image = splicing(data.image);
        data.dateTime = formatTime(data.dateTime);
        this.setData({
          newsDetails: data
        });
      };
    });
  },

  // 收藏点赞
  actions(e) {
    let {
      type
    } = e.currentTarget.dataset;
    if (app.globalData.isAuthorization) {
      if (type === 'good') {
        this.goodNews();
      } else {
        this.collectNews();
      };
    } else {
      if (type === 'good') {
        this.setData({
          authorizationCallBack: 'good'
        });
      } else {
        this.setData({
          authorizationCallBack: 'collect'
        });
      };
      this.setData({
        authorizationShow: true
      });
    };
  },
  callBack(e) {
    if (e.detail.type === 'good') {
      this.goodNews();
    } else {
      this.collectNews();
    };
  },

  // 点赞
  goodNews() {
    wx.showLoading({
      title: '请稍候...',
      mask: true
    });
    likeNews(this.data.newsId).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        if (this.data.newsDetails.hasLike) {
          wx.showToast({
            title: '取消成功',
            icon: 'none'
          });
          this.setData({
            'newsDetails.hasLike': false,
            'newsDetails.likes': this.data.newsDetails.likes - 1
          });
        } else {
          wx.showToast({
            title: '点赞成功',
            icon: 'success'
          });
          this.setData({
            'newsDetails.hasLike': true,
            'newsDetails.likes': this.data.newsDetails.likes + 1
          });
        };
      };
    });
  },

  // 收藏
  collectNews() {
    wx.showLoading({
      title: '请稍候...',
      mask: true
    });
    collectNews(this.data.newsId).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        if (this.data.newsDetails.hasCollect) {
          wx.showToast({
            title: '取消成功',
            icon: 'none'
          });
          this.setData({
            'newsDetails.hasCollect': false,
            'newsDetails.collects': this.data.newsDetails.collects - 1
          });
        } else {
          wx.showToast({
            title: '收藏成功',
            icon: 'success'
          });
          this.setData({
            'newsDetails.hasCollect': true,
            'newsDetails.collects': this.data.newsDetails.collects + 1
          });
        };
      };
    });
  }
})